﻿// Decompiled with JetBrains decompiler
// Type: LIB.IniHelper
// Assembly: StartRevit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 5928FEFF-973F-47EB-B50D-97567B9E8CD8
// Assembly location: D:\Test\StartRevit.exe

using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace RevitStarter.Util
{
    public static class IniHelper
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        public static void WriteSetting(string section, string key, string value, string filePath)
        {
            WritePrivateProfileString(section, key, value, filePath);
        }

        public static void WriteSetting(string section, string key, int value, string filePath)
        {
            WritePrivateProfileString(section, key, value.ToString(), filePath);
        }

        public static void WriteSetting(string section, string key, double value, string filePath)
        {
            WritePrivateProfileString(section, key, value.ToString(), filePath);
        }


        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public static string GetSetting(string Section, string Key, string filePath, string valueDef = "")
        {
            if (!File.Exists(filePath))
                return valueDef;
            int num = 12288;
            string str = valueDef;
            StringBuilder retVal = new StringBuilder(num);
            if (GetPrivateProfileString(Section, Key, "", retVal, num, filePath) != 0)
                str = retVal.ToString();
            return str;
        }

        public static int GetIntSetting(string Section, string Key, string ConfigFile, int valueDef = 0)
        {
            string valueDef1 = valueDef.ToString();
            string setting = GetSetting(Section, Key, ConfigFile, valueDef1);
            int result;
            int.TryParse(setting, out result);
            return result;
        }

        public static double GetDoubleSetting(string Section, string Key, string ConfigFile, double valueDef = 0.0)
        {
            string valueDef1 = valueDef.ToString();
            string setting = GetSetting(Section, Key, ConfigFile, valueDef1);
            double result;
            double.TryParse(setting, out result);
            return result;
        }
    }
}